package com.hermit.qmst.UI.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hermit.qmst.R;
import com.hermit.qmst.tools.Common;
import com.hermit.qmst.tools.Preferences;
import com.hermit.qmst.tools.RequestHttp;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created by Administrator on 2017/1/3 0003.
 */

@ContentView(R.layout.new_activity_register)
public class RegisterActivity extends Activity implements View.OnClickListener {


    @ViewInject(R.id.iv_back)
    ImageView mIvBack;
    @ViewInject(R.id.tv_title)
    TextView mTvTitle;
    @ViewInject(R.id.cb_nan)
    CheckBox mCbNan;
    @ViewInject(R.id.cb_nv)
    CheckBox mCbNv;
    @ViewInject(R.id.et_borthday)
    EditText mEtBorthday;
    @ViewInject(R.id.et_adress)
    EditText mEtAdress;
    @ViewInject(R.id.bt_save)
    Button mBtSave;
    private String sex = "男";
    private String registerUrl = Common.mUrl + "szdh/userreg.php?username=" + VerifyPhoneActivity.phone + "&password=" + ForegetPwdActivity.pwd + "&acct=" + ForegetPwdActivity.code + "&address=";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        mIvBack.setOnClickListener(this);
        mBtSave.setOnClickListener(this);
        mTvTitle.setText("用户信息");
        mCbNan.setOnClickListener(this);
        mCbNv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_save:
                save();
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.cb_nv:
                mCbNan.setChecked(false);
                mCbNv.setChecked(true);
                sex = "女";
                break;
            case R.id.cb_nan:
                mCbNan.setChecked(true);
                mCbNv.setChecked(false);
                sex = "男";
                break;
        }
    }

    private void save() {
        if (mCbNan.isChecked()) {
            sex = "女";
        }
        String adress = "";
        String borthday = "";
        String sexs = "";
        try {
            adress = URLEncoder.encode(mEtAdress.getText().toString(), "utf-8");
            borthday = URLEncoder.encode(mEtBorthday.getText().toString(), "utf-8");
            sexs = URLEncoder.encode(sex, "utf-8");
        } catch (UnsupportedEncodingException e) {
        }
        String url = adress + "&sex=" + sexs + "&birthday=" + borthday;
        RequestHttp.getHttp(this, registerUrl + url, new RequestHttp.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject) {
                Preferences p = new Preferences(RegisterActivity.this);
                try {
                    Toast.makeText(RegisterActivity.this, jsonObject.getString("error"), Toast.LENGTH_SHORT).show();
                    if (jsonObject.getBoolean("state")) {
//                        JSONObject object = jsonObject.getJSONArray("data").getJSONObject(0);
                        p.setPreferenceStringValue(Preferences.MY_PHONE, VerifyPhoneActivity.phone);
                        p.setPreferenceStringValue(Preferences.MY_PASSWORD, ForegetPwdActivity.pwd);
                        //直拨账号
                        p.setPreferenceStringValue(Preferences.SIP_ACCOUNT, "500" + VerifyPhoneActivity.phone);
                        p.setPreferenceStringValue(Preferences.SIP_AGENT, jsonObject.getString("agent_id"));
                        p.setPreferenceStringValue(Preferences.SIP_AGENT_THIRD, jsonObject.getString("agent_id_third"));
                        p.setPreferenceStringValue(Preferences.USER_ID, jsonObject.getString("id"));
                        Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
                        startActivity(intent);
                        RegisterActivity.this.finish();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
